package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.DockDAO;
import com.chanyachting.emall.domain.Dock;
import com.chanyachting.emall.service.DockyachtService;

public class DockyachtServiceImpl implements DockyachtService {

	private DockDAO cDao;

	public DockyachtServiceImpl() {

		this.cDao = new DockDAO();
	}
	
	public int QueryAllDockCountByChannelId(String channelId) {
		String sql = "select count(*) from Dock where recommend = '1' and channelId in (" + channelId + ")";
		
		return (Integer)this.cDao.find(sql).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Dock> QueryAllDockByChannelId(int currentPage,int pageSize,String channelId) {
		System.out.println("channelId=" + channelId);
		String sql = "from Dock where recommend = '1' and channelId in (" + channelId + ")";
		sql = sql + " order by updateDate desc";
		Query q = this.cDao.createQuery(sql);
		q.setMaxResults(pageSize);
		q.setFirstResult((currentPage -1) * pageSize);
		
		return q.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Dock> QueryAppointSellYacht(int currentPage,int pageSize,String productCategoryId) {
		String sql= "from Dock where " +
				"status = '01' and productCategoryId = '" + productCategoryId +"'";
		Query q = this.cDao.createQuery(sql);
		q.setMaxResults(pageSize);
		q.setFirstResult((currentPage -1) * pageSize);
		
		return q.list();
	}
	
	public int QueryAppointSellYachtCount(String status,String productCategoryId) {
		String sql = "select count(*) from Dock " +
			"where status = ? and productCategoryId = ?";
		
		return (Integer)this.cDao.find(sql, new String[]{status,productCategoryId}).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Dock QueryOneDock(String productId) {
		String sql = "from Dock where id = ?";
		
		List<Dock> l = this.cDao.find(sql, new String[]{productId});
		
		if (l == null || l.size() != 1) {
			return null;
		}
		return l.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Dock> QueryDockByCompanyId(String companyId) {
		String sql = "from Dock where companyId = ? and status = '01'";
		
		List<Dock> l = this.cDao.find(sql, new String[]{companyId});
		
		return l;
	}
	
	@SuppressWarnings("unchecked")
	public List<Dock> SearchDockByChannel(int currentPage,int pageSize,String channelId,Dock d,String bearchNum) {
		StringBuffer sqls = new StringBuffer();
		sqls.append("from Dock where recommend = '1' and channelId in (" + channelId + ")");
		sqls.append(" and title like ?");
		if (!"".equals(bearchNum)) {
			sqls.append(" and berthNumber " + bearchNum);
		}
		if (null != d.getAddress() && !"".equals(d.getAddress())) {
			sqls.append(" and address = ?");
		}
		if (null != d.getBerthType() && !"".equals(d.getBerthType())) {
			sqls.append(" and berthType = ?");
		}
		sqls.append(" order by updateDate desc");
		Query q = this.cDao.createQuery(sqls.toString());
		q.setMaxResults(pageSize);
		q.setFirstResult((currentPage -1) * pageSize);
		q.setString(0, "%" + d.getTitle() + "%");
		
		if (null != d.getAddress() && !"".equals(d.getAddress())
				&& null != d.getBerthType() && !"".equals(d.getBerthType())) {
			q.setString(1, d.getAddress());
			q.setString(2, d.getBerthType());
		} else if (null != d.getAddress() && !"".equals(d.getAddress())) {
			q.setString(1, d.getAddress());
		}else if (null != d.getBerthType() && !"".equals(d.getBerthType())) {
			q.setString(1, d.getBerthType());
		}
		
		return q.list();
	}
	
	public int SearchDockCountByChannel(String channelId,Dock d,String bearchNum) {
		StringBuffer sqls = new StringBuffer();
		sqls.append("select count(*) from Dock where recommend = '1' and channelId in (" + channelId + ")");
		sqls.append(" and title like ?");
		if (!"".equals(bearchNum)) {
			sqls.append(" and berthNumber " + bearchNum);
		}
		if (null != d.getAddress() && !"".equals(d.getAddress())) {
			sqls.append(" and address = ?");
		}
		if (null != d.getBerthType() && !"".equals(d.getBerthType())) {
			sqls.append(" and berthType = ?");
		}
		sqls.append(" order by updateDate desc");
		Query q = this.cDao.createQuery(sqls.toString());
		q.setString(0, "%" + d.getTitle() + "%");
		
		if (null != d.getAddress() && !"".equals(d.getAddress())
				&& null != d.getBerthType() && !"".equals(d.getBerthType())) {
			q.setString(1, d.getAddress());
			q.setString(2, d.getBerthType());
		} else if (null != d.getAddress() && !"".equals(d.getAddress())) {
			q.setString(1, d.getAddress());
		}else if (null != d.getBerthType() && !"".equals(d.getBerthType())) {
			q.setString(1, d.getBerthType());
		}
		
		return (Integer)q.list().get(0);
	}
}
